package com.fone.player.storage;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import com.fone.player.entity.OfflineCache;
import com.fone.player.util.L;

/**
 * 
 * 缓存表适配器
 * 
 * @author wangyang
 * @since 2014年4月29日
 */
public class OfflineCacheDataBaseAdapter implements IFoneDatabase {
	private static final String TAG = OfflineCacheDataBaseAdapter.class
			.getSimpleName();
	private static final OfflineCacheDataBaseAdapter mInstance = new OfflineCacheDataBaseAdapter();
	private OperateDataBaseTemplate mGeneralDataBaseTemplate = OperateDataBaseTemplate
			.getInstance();

	private OfflineCacheDataBaseAdapter() {

	}

	public static OfflineCacheDataBaseAdapter getInstance() {
		return mInstance;
	}


	/**
	 * 
	 * 添加缓存条目
	 * 
	 * @param offlineCache
	 *            缓存条目
	 * @return int 插入结果<br>
	 *         1 成功 <br>
	 *         -1 插入异常
	 * 
	 */
	public int addOfflineCacheFolder(OfflineCache offlineCache) {
		synchronized (TAG) {

			// 创建文件夹sql语句
			StringBuilder sql = new StringBuilder();
			sql.append("insert or ignore into ")
					.append(TB_OFFLINE_CACHE_FOLDER).append(" (");
			sql.append(OFFLINE_CACHE_FOLDER_CID).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_CCID).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_VIDEO_ID).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_NAME).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_IMAGE_URL).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_EPISODE_NUM).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_EPISODE_UPDATE_INFO).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_CONTENT_TYPE).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_XYZPLAY_URL).append(",");
			sql.append(OFFLINE_CACHE_FOLDER_DRAMA_URL);
			sql.append(") values(?,?,?,?,?,?,?,?,?,?);");

			try {
				mGeneralDataBaseTemplate.open();
				mGeneralDataBaseTemplate.beginTransaction();

				SQLiteStatement fileStatement = mGeneralDataBaseTemplate
						.getSQLiteStatement(sql.toString());

				fileStatement.bindLong(1, offlineCache.getCacheCID());
				fileStatement.bindLong(2, offlineCache.getCacheCCID());
				fileStatement.bindLong(3, offlineCache.getCacheVideoId());
				fileStatement.bindString(4, offlineCache.getCacheName());
				fileStatement.bindString(5, offlineCache.getCacheImageUrl());
				fileStatement.bindString(6, offlineCache.getCacheEpisodeNum());
				fileStatement.bindString(7,
						offlineCache.getCacheEpisodeUpdateInfo());
				fileStatement.bindLong(8, offlineCache.getCacheContentType());
				fileStatement.bindString(9, offlineCache.getCacheXYZPlayUrl());
				fileStatement.bindString(10, offlineCache.getCacheDramaUrl());
				mGeneralDataBaseTemplate.insert(fileStatement);

				mGeneralDataBaseTemplate.setTransactionSuccessful();

			} catch (Exception e) {
				e.printStackTrace();
				L.v(TAG, e.getMessage());
				return -1;
			} finally {
				mGeneralDataBaseTemplate.endTransaction();
				mGeneralDataBaseTemplate.close();
			}
			return 1;
		}
	}

	/**
	 * 
	 * 获得缓存集合
	 * 
	 * @return ArrayList<OfflineCache> 缓存集合
	 */
	ArrayList<OfflineCache> getOfflineCacheList() {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_OFFLINE_CACHE_FOLDER);
		ArrayList<OfflineCache> offlineCacheList = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			offlineCacheList = new ArrayList<OfflineCache>();
			while (cursor.moveToNext()) {
				OfflineCache offlineCache = new OfflineCache();
				offlineCache.setCacheCID(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_CID)));
				offlineCache.setCacheCCID(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_CCID)));
				offlineCache.setCacheVideoId(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_VIDEO_ID)));
				offlineCache.setCacheAlreadySize(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_NAME)));
				offlineCache.setCacheImageUrl(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_IMAGE_URL)));
				offlineCache.setCacheEpisodeNum(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_EPISODE_NUM)));
				offlineCache
						.setCacheEpisodeUpdateInfo(cursor.getString(cursor
								.getColumnIndex(OFFLINE_CACHE_FOLDER_EPISODE_UPDATE_INFO)));
				int contentType = cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_CONTENT_TYPE));
				offlineCache.setCacheContentType(contentType);
				switch (contentType) {
				case OfflineCache.CACHE_CONTENT_TYPE_ANIMATION:
				case OfflineCache.CACHE_CONTENT_TYPE_TV:
				case OfflineCache.CACHE_CONTENT_TYPE_VARIETY:
					// 文件夹
					offlineCache
							.setCacheHierarchyType(OfflineCache.CACHE_HIERARCHY_FOLDER);
					break;
				default:
					// 文件
					offlineCache
							.setCacheHierarchyType(OfflineCache.CACHE_HIERARCHY_FILE);
					break;
				}

				offlineCache.setCacheXYZPlayUrl(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_XYZPLAY_URL)));
				offlineCache.setCacheDramaUrl(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FOLDER_DRAMA_URL)));
				offlineCacheList.add(offlineCache);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		return offlineCacheList;
	}

	/**
	 * 
	 * 根据缓存ID获取缓存集合
	 * 
	 * @return ArrayList<OfflineCache> 缓存集合
	 */
	public ArrayList<OfflineCache> getOfflineCacheFileListByCID(long CID) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_OFFLINE_CACHE_FILE);
		sql.append(" where ");
		sql.append(OFFLINE_CACHE_FILE_CID);
		sql.append("=");
		sql.append(CID);
		ArrayList<OfflineCache> offlineCacheList = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			offlineCacheList = new ArrayList<OfflineCache>();
			while (cursor.moveToNext()) {
				OfflineCache offlineCache = new OfflineCache();
				offlineCache.setCacheCID(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_CID)));
				offlineCache.setCacheCCID(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_CCID)));
				offlineCache.setCacheName(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_NAME)));
				offlineCache.setCacheImageUrl(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_IMAGE_URL)));
				offlineCache.setCacheEpisodeNum(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_EPISODE_NUM)));
				offlineCache.setCacheAlreadySize(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_ALREADY_SIZE)));
				offlineCache.setCacheTotalSize(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_TOTAL_SIZE)));
				offlineCache.setCacheDownloadState(cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_DOWNLOAD_STATE)));
				offlineCache.setCacheContentType(cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_CONTENT_TYPE)));
				offlineCache.setCacheDefinitionType(cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_DEFINITION_TYPE)));
				offlineCache.setCacheDownloadType(cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_DOWNLOAD_TYPE)));
				offlineCache.setCacheDuration(cursor.getLong(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_DURATION)));
				offlineCache.setCacheFragmentCount(cursor.getInt(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_FRAGMENT_COUNT)));
				offlineCache.setCacheStoragePath(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_STORAGE_PATH)));
				offlineCache.setCacheXYZPlayUrl(cursor.getString(cursor
						.getColumnIndex(OFFLINE_CACHE_FILE_XYZPLAY_URL)));
				offlineCacheList.add(offlineCache);
			}
			cursor.close();
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}
		return offlineCacheList;
	}

	/**
	 * 
	 * 根据Id删除缓存 <br>
	 * 同步方法
	 * 
	 * @param cacheId
	 *            缓存ID
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteOfflineCacheFolderByCID(long CID) {
		int result = 1;
		try {

			StringBuilder sql = new StringBuilder();
			sql.append("delete from ").append(TB_OFFLINE_CACHE_FOLDER);
			sql.append(" where ");
			sql.append(OFFLINE_CACHE_FOLDER_CID).append(" = ");
			sql.append(CID);
			mGeneralDataBaseTemplate.open();
			mGeneralDataBaseTemplate.delete(sql.toString());

		} catch (Exception e) {
			result = -1;
		} finally {
			mGeneralDataBaseTemplate.close();
		}
		return result;
	}

}
